<?php
/**
 * Created by PhpStorm.
 * User: yuanluke
 * Date: 3/14/17
 * Time: 23:34
 */

namespace App\Http\Utils\ControllerTrait;

use Illuminate\Http\Request;
use App\Http\Requests;
use Log;
use App\Http\Utils\Common\OperResult;

trait RichUploadTrait {

    /**
     * upload image limited by size(200K) & extension name(png,jpg,jpeg,gif)
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadImageBackend(Request $req) {
        try {

            if ($req->hasFile('filedata') && $req->file('filedata')->isValid()) {
                $fileData = $req->file('filedata');
                $fExtName = strtolower($fileData->getClientOriginalExtension());
                $fSize = $fileData->getClientSize();

                if ($fExtName!='png' && $fExtName!='jpg' && $fExtName!='jpeg' && $fExtName!='gif') {
                    return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_IMG_EXT_FAIL_DESC]);
                }
                if ($fSize>OperResult::UPLOAD_IMG_MAX_SIZE*1024) {
                    return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_IMG_SIZE_FAIL_DESC]);
                }

                $fName = uniqid(true).'.'.$fExtName;
                $saveDir = base_path().'/public/assets/up/'.$req->input('destpos').'/';
                $fileData->move($saveDir, $fName);
                return response()->json(['error'=>0, 'url'=>'/assets/up/'.$req->input('destpos').'/'.$fName, 'fname'=>$fName]);
            } else {
                return response()->json(['error'=>-1]);
            }

        } catch (Exception $ex) {
            Log::error('[上传文件失败] : '.$ex->getMessage().$ex->getTraceAsString());
            return response()->json(['error'=>-1, 'message'=>OperResult::CODE_UPLOAD_FAIL_DESC]);
        }
    }




    /**
     * upload musicfiel limited by size(20MB) & extension name(mp3, mp4, mid, wav, wma, rm)
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadFilemusicBackend(Request $req) {
        try {
            Log::debug('上传状态: '.$req->hasFile('filedata').'   '.$req->file('filedata')->isValid());
            if ($req->hasFile('filedata') && $req->file('filedata')->isValid()) {
                $fileData = $req->file('filedata');
                $fExtName = strtolower($fileData->getClientOriginalExtension());
                $fSize = $fileData->getClientSize();

                if ($fExtName!='mp3' && $fExtName!='mp4' && $fExtName!='mid' && $fExtName!='wav' && $fExtName!='wma' && $fExtName!='rm') {
                    return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_MUSIC_EXT_FAIL_DESC]);
                }
                if ($fSize>20*1024*1024) {
                    return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_FILE_SIZE_FAIL_DESC]);
                }

                $fName = uniqid(true).'.'.$fExtName;
                $saveDir = base_path().'/public/assets/up/'.$req->input('destpos').'/';
                Log::debug('save to: '.$saveDir);
                $fileData->move($saveDir, $fName);
                return response()->json(['error'=>0, 'url'=>'/assets/up/'.$req->input('destpos').'/'.$fName, 'fname'=>$fName]);
            } else {
                return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_FAIL_DESC]);
            }

        } catch (Exception $ex) {
            Log::error('[上传文件失败] : '.$ex->getMessage().$ex->getTraceAsString());
            return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_FAIL_DESC]);
        }
    }




    /**
     * upload file limited by size(200K) & extension name(png,jpg,jpeg,gif)
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadFileBackend(Request $req) {
        try {
            if ($req->hasFile('filedata') && $req->file('filedata')->isValid()) {
                $fileData = $req->file('filedata');
                $fExtName = strtolower($fileData->getExtension());
                $fSize = $fileData->getClientSize();

                if ($fExtName!='png' && $fExtName!='jpg' && $fExtName!='jpeg' && $fExtName!='gif') {
                    return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_IMG_EXT_FAIL_DESC]);
                }
                if ($fSize>200*1024) {
                    return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_FILE_SIZE_FAIL_DESC]);
                }

                $fName = uniqid(true).'.'.fExtName;
                $saveDir = base_path().'/public/assets/up/'.$req->input('destpos').'/';
                $fileData->move($saveDir, $fName);
                return response()->json(['error'=>0, 'url'=>$saveDir.$fName]);
            } else {

            }

        } catch (Exception $ex) {
            Log::error('[上传文件失败] : '.$ex->getMessage().$ex->getTraceAsString());
            return response()->json(['error'=>1, 'message'=>OperResult::CODE_UPLOAD_FAIL_DESC]);
        }
    }

}